var currentSegment="MSW_07000";
var rClickImg = 0;
var kxPos,kyPos;
var hsArea=false;
var qCtrl,segID;
var textFieldValue;
var slidFlag=false;

function loadQuestions(qctrl, sID){
	qCtrl = qctrl;
	segID = sID;		
	drawHotspots();
	slide(qCtrl);
}

function drawHotspots(){	
	console.log("in draw....")
	$('#questioncontainer').html('');
	
	var curItem, newItem;	
	for(var i=0;i<questionHotspots.length;i++){
		if(questionHotspots[i].id==segID){
			for(z in questionHotspots[i].htmls) {
				curItem = questionHotspots[i].htmls[z];
				newItem = $(curItem[4]);
				newItem.css({
								position:"absolute",
								border: "0px",
								"z-index": 9,
								left: curItem[0]+"px",
								top: curItem[1]+"px",
								width: (curItem[2] - curItem[0])+"px",
								height: (curItem[3] - curItem[1])+"px"
				});
				$('#questioncontainer').append(newItem);
			}
			for(j in questionHotspots[i].marks) {
				curItem = questionHotspots[i].marks[j];
				newItem = $("<div></div>");
				newItem.attr("id", j);
				
				newItem.attr("onclick", "doClickHandlerInQues(event, this);correctClick=true;");
				newItem.attr("onmouseover", "doOverActionInQues(event, this)");
				newItem.attr("onmouseout", "doOutActionInQues(event, this)");
				
				newItem.bind("contextmenu",function(e){
					console.log("rrrrrrrrrrrrrrrrrrr")
					actionOffice(e, this);
					return false;			
				});
				
				newItem.css({"position":"absolute","display":"block","z-index":"2000","left":curItem[0]+"px","top":curItem[1]+"px","width":(curItem[2]-curItem[0])+"px",						"height": (curItem[3] - curItem[1])+"px"});
				if(j.substring(0,1)=="s"){hsArea=true;newItem.css({"background-color":"red","opacity":"0.2"})}
				$('#questioncontainer').append(newItem);
			}
			for(var k in questionHotspots[i].action) {
				actionType = k.substring(0,1);
				
				if ('hB'.indexOf(actionType)>=0) {
					curItem = questionHotspots[i].action[k];
					newItem = $("<div></div>");
					newItem.attr("id", k);
					newItem.css({
									display: "none",
									"background-color":(actionType=='h'?"LightSteelBlue":""),
									border:(actionType=='b'?"1px solid lightblue":(actionType=='B'?"5px solid LightSteelBlue":"0px")),
									'box-sizing': 'border-box',
									opacity: 0.5,
									position:"absolute",
									left: curItem[0]+"px",
									top: curItem[1]+"px",
									width: (curItem[2] - curItem[0])+"px",
									height: (curItem[3] - curItem[1])+"px"
					});					
					$('#questioncontainer').append(newItem);
				}
			};
			screenPage=i;
			break;
		}	
	}
}

function slide(qCtrl){
	if(currentSegment=="MSW_07010"){isShowDropdown=true;$("#glasscontainer").html('')};
	if(currentSegment=="MSW_RESULT"){$("#previewshow").remove()};
}

function doRightClickUsingKeys(ctrl){
	console.log("doRightClickUsingKeys..."+currentSegment)
	if(currentSegment == "MSW_05000"){
		kxPos=265;kyPos=130;
		doRightClickHandler(ctrl)
	}
}

function doClickHandlerInQues(event, ctrl){
	if(!isShowDropdown){
		hsArea=false;
	}
	actionOffice(event, ctrl)			
}

$(document).bind("dblclick",function(e) {
	
});
		
$(document).keyup(function(e){
	
	if(e.shiftKey && e.which==121){
		incorrectMsg=false;
	}if(currentSegment=="MSW_07011"){isShowDropdown=true;isShowRightClickMenu=false;}
});

function doOverActionInQues(event, ctrl) {	
	
	if(ctrl.id=="m_None" || 
	ctrl.id=="m_FilledCircle" || 
	ctrl.id=="m_Circle" || 
	ctrl.id=="m_TriColor" || 
	ctrl.id=="m_Diamond" || 
	ctrl.id=="m_Arrow" || 
	ctrl.id=="m_Checkmark" || 
	ctrl.id=="m_FilledSquare"||
	ctrl.id=="m_NoneRC" || 
	ctrl.id=="m_FilledCircleRC" || 
	ctrl.id=="m_CircleRC" || 
	ctrl.id=="m_TriColorRC" || 
	ctrl.id=="m_DiamondRC" || 
	ctrl.id=="m_ArrowRC" || 
	ctrl.id=="m_CheckmarkRC" || 
	ctrl.id=="m_FilledSquareRC"){		
		var preview = "../images/item07/"+ctrl.id.substring(2)+"_preview.png";		
		$("#maincontainer").append('<img id="previewshow" style="position:absolute;display:block;z-index:96;left:156px;top:219px;">')
		$('#previewshow').attr('src',preview);		
	}else{$('#previewshow').remove();}
	doAllActionInQues(event, ctrl, 5);
	clearKeyHighlightsInDropdown();
}

function doOutActionInQues(event, ctrl) {
	doAllActionInQues(event, ctrl, 5, true);
}

function doAllActionInQues(event, ctrl, idx, isActionRevert) {
	//console.log("actionList in ques="+ctrl.id+""+idx)
	var actionList = questionHotspots[screenPage].marks[ctrl.id][idx];	
	
	for(var i=0;i<actionList.length;i++) {
		doAction(event, actionList[i], isActionRevert);
	}
}

var liItem,arrKeyCount=0;

function doDropdownkeyHandler(keycode){
	console.log(isShowDropdown+"doDropdownkeyHandler..."+canTakeKeyDown)
	//incorrectMsg=false;
	var curItem,liItem=[];
	
	for(var i in questionHotspots[screenPage].marks) {
		curItem = i.substring(2)		
		liItem.push(curItem)		
	}
	
	if(keycode==40){//downArrow
		$("#m_"+liItem[arrKeyCount]).css({"background-color":"LightSteelBlue","opacity":"0"});	
		arrKeyCount++;
	}else if(keycode==38){//upArrow		
		$("#m_"+liItem[arrKeyCount]).css({"background-color":"LightSteelBlue","opacity":"0"});		
		arrKeyCount--;
		if(arrKeyCount<0){arrKeyCount=liItem.length;}
	}
	if(liItem.length>arrKeyCount){		
		$("#m_"+liItem[arrKeyCount]).css({"background-color":"LightSteelBlue","opacity":"0.5"});	
	}else{
		if(keycode==40){
			arrKeyCount=0;
		}else if(keycode==38){
			arrKeyCount=liItem.length-1;
		}
		$("#m_"+liItem[arrKeyCount]).css({"background-color":"LightSteelBlue","opacity":"0.5"});
	}
	if(currentSegment=="MSW_05011")
	$("input[id=t_StyleNameTxt]").attr("value",liItem[arrKeyCount]);
	
	rightClickKeyHandler(keycode,true, liItem[arrKeyCount])
}


function clearKeyHighlightsInDropdown(){
	for(var i in questionHotspots[screenPage].marks) {
		curItem = i.substring(2)		
		$("#m_"+curItem).css({"background-color":"LightSteelBlue","opacity":"0"});
	}
}